package cn.sdormitory.knowledgebase.service.impl;

import cn.sdormitory.knowledgebase.dao.StudyQuestionsDao;
import cn.sdormitory.knowledgebase.pojo.StudyQuestions;
import cn.sdormitory.knowledgebase.service.StudyQuestionsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class StudyQuestionsServiceImpl extends ServiceImpl<StudyQuestionsDao, StudyQuestions> implements StudyQuestionsService {

    @Resource
    private StudyQuestionsDao studyQuestionsDao;


    @Override
    public IPage<StudyQuestions> getList(String content, Integer type,Integer category, Integer pageNum, Integer pageSize) {
        // 创建分页对象
        Page<StudyQuestions> page = new Page<>(pageNum, pageSize);

        // 构建查询条件
        LambdaQueryWrapper<StudyQuestions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type != null && type != 0,
                        StudyQuestions::getType, type)
                .eq(category != null && category != 0,
                        StudyQuestions::getCategory, category)
                .like(StringUtils.isNotBlank(content),
                        StudyQuestions::getContent, content);
        wrapper.orderByDesc(StudyQuestions::getCreateTime);

        // 执行分页查询
        Page<StudyQuestions> studyQuestionsPage = studyQuestionsDao.selectPage(page, wrapper);

        // 转换为VO对象
        Page<StudyQuestions> voPage = new Page<>();
        BeanUtils.copyProperties(studyQuestionsPage, voPage, "records");

        // 转换记录
        List<StudyQuestions> voList = studyQuestionsPage.getRecords().stream()
                .map(entity -> {
                    StudyQuestions studyQuestions = new StudyQuestions();
                    BeanUtils.copyProperties(entity, studyQuestions);
                    return studyQuestions;
                })
                .collect(Collectors.toList());

        voPage.setRecords(voList);

        return voPage;
    }

    @Override
    public Integer addStudyQuestions(StudyQuestions studyQuestions) {
        return studyQuestionsDao.insert(studyQuestions);
    }

    @Override
    public StudyQuestions getStudyQuestions(Integer id) {
        return studyQuestionsDao.selectById(id);
    }


    @Override
    public Integer updateStudyQuestions(StudyQuestions studyQuestions) {
        return studyQuestionsDao.updateById(studyQuestions);
    }

    @Override
    public Integer deleteStudyQuestions(Integer id) {
        return studyQuestionsDao.deleteById(id);
    }

    @Override
    public Integer deleteStudyQuestionsByIds(Integer[] ids) {
        List<Integer> idList = Arrays.asList(ids);
        return studyQuestionsDao.deleteBatchIds(idList);
    }

}
